//
//  GBAppDelegate.m
//  XcodeAssist
//
//  Created by wanglidong on 14-5-9.
//  Copyright (c) 2014年 GoldenBull. All rights reserved.
//

#import "GBAppDelegate.h"
#import "GBNSUtils.h"

@implementation GBAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    [GBNSUtils addTarget:self action:@selector(onRun) forControl:_buttonRun];
    
    
    [GBNSUtils addTarget:self action:@selector(onOpenPath) forControl:_buttonPath];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
    return YES;
}

- (void)onOpenPath
{
    [GBNSUtils openFolder:NO completionHandler:^(NSInteger result, NSString *path) {
        
        if (result != 1) {
            return;
        }
        
        
        _textFieldPath.stringValue = path;
    }];
}

- (void)onRun
{
    if ([[_textViewContent string] length] < 1) {
        return;
    }
    
    if ([[_textFieldPath stringValue] length] < 1) {
        return;
    }
    
    [self parseWarnings];
}

- (void)parseWarnings
{
    NSMutableString *content = [NSMutableString stringWithString:[_textViewContent string]];
    NSRange range = NSMakeRange(0, content.length);
    
    [content replaceOccurrencesOfString:@"\n\n" withString:@"\n" options:NSCaseInsensitiveSearch range:range];
    
    NSArray *list = [content componentsSeparatedByString:@"\n"];
    
    NSString *path = [_textFieldPath stringValue];
    
    NSFileManager *fm = [NSFileManager defaultManager];
    
    for (NSString *sw in list)
    {
        NSString *name = [sw lastPathComponent];
        NSString *file = [path stringByAppendingPathComponent:name];
        
        [fm removeItemAtPath:file error:nil];
        
        [self showLog:[NSString stringWithFormat:@"已删除:%@",file]];
    }
}

- (void)showLog:(NSString *)log
{
    NSString *s = [_textViewLog string];
    
    _textViewLog.string = [s stringByAppendingFormat:@"\n%@",log];
}

@end

/*
 
 
 Warning: Multiple build commands for output file /Users/wanglidong/Library/Developer/Xcode/DerivedData/XcodeAssist-bsetlofeolfbpkcfjirxruhihenk/Build/Products/Debug/XcodeAssistTests.xctest/Contents/Resources/battle_bg_1.png
 Warning: Multiple build commands for output file /Users/wanglidong/Library/Developer/Xcode/DerivedData/XcodeAssist-bsetlofeolfbpkcfjirxruhihenk/Build/Products/Debug/XcodeAssistTests.xctest/Contents/Resources/baoshijia2.png
 Warning: Multiple build commands for output file /Users/wanglidong/Library/Developer/Xcode/DerivedData/XcodeAssist-bsetlofeolfbpkcfjirxruhihenk/Build/Products/Debug/XcodeAssistTests.xctest/Contents/Resources/baoshishu.png
 Warning: Multiple build commands for output file /Users/wanglidong/Library/Developer/Xcode/DerivedData/XcodeAssist-bsetlofeolfbpkcfjirxruhihenk/Build/Products/Debug/XcodeAssistTests.xctest/Contents/Resources/baoshijia.png
 Warning: Multiple build commands for output file /Users/wanglidong/Library/Developer/Xcode/DerivedData/XcodeAssist-bsetlofeolfbpkcfjirxruhihenk/Build/Products/Debug/XcodeAssistTests.xctest/Contents/Resources/baoshi.png
 */
